package ua.kpi_java_training.gluschenko.module1;
import java.util.Scanner;

public class division {

    static Integer dividend=0;
    static Integer divisor=0;

    static String dividendStr="";
    static String divisorStr="";

    static String workPartStr="";
    static int workPart=0;
    static int finalRes=0;

    static int product=0;
    static int rest=0;
    static int position=0;

    enum Step{FIRST,OTHER,LAST}
    enum Sign{POZITIVE,NEGATIVE,INCORRECT}
    static Step step;

    public static void main(String[] args) {
        input();
        doDivision();
        print("");
    }

     static Sign isNumber(String str){
         if(str.matches("[0-9]*")){
             return Sign.POZITIVE;
         }else if(str.matches("-[0-9]*")){
             return Sign.NEGATIVE;
         }else{
             return Sign.INCORRECT;
         }
     }

    static void input(){
        Scanner in = new Scanner(System.in);
        do{
            System.out.print("Please enter dividend: ");
            dividendStr = in.nextLine();
        }while(isNumber(dividendStr)==Sign.INCORRECT);

        do{
            System.out.println("Please enter divisor: ");
            divisorStr=in.nextLine();
        }while(isDivisorValid());

        dividend=parse(dividendStr);
        divisor=parse(divisorStr);
    }

    static public boolean isDivisorValid(){
        return isNumber(divisorStr)==Sign.INCORRECT||Integer.parseInt(divisorStr)==0;
    }

    private static int parse(String str){
        if(isNumber(str)==Sign.NEGATIVE){
            return (Integer.parseInt(str.substring(1)))*(-1);
        }  else {
            return Integer.parseInt(str);
        }
    }
    static  void doDivision(){
        int lengthDividend=dividendStr.length();
        int lengthDivisor=divisorStr.length();

        if(lengthDividend>lengthDivisor){
           doZeroStep();
           print(workPartStr);

            while(position<lengthDividend){
                String str=dividendStr.substring(position,++position);
                workPartStr=Integer.toString(rest)+str;

                doOneStepDivision();
                print(workPartStr);
           }
           step=Step.LAST;
       }else if(lengthDividend<lengthDivisor){
          System.out.println("Final result is 0, remainder of the division is  "+ Math.abs(dividend));
       }
    }

    static void doZeroStep(){
        int lengthDivisor=divisorStr.length();
        workPartStr=dividendStr.substring(0,lengthDivisor);
        if(Integer.parseInt(workPartStr)>divisor){
            position=lengthDivisor;
            doOneStepDivision();
        }else{
            position=lengthDivisor+1;
            workPartStr=dividendStr.substring(0,position);
            doOneStepDivision();
        }
        step=Step.FIRST;
    }

    static void doOneStepDivision(){
        int res=0;

        step=Step.OTHER;
        workPart=Integer.parseInt(workPartStr);
        res=workPart/divisor;
        product=res*divisor;
        rest=workPart-product;
        finalRes=finalRes*10+res;
    }

    static void print(String workPartStr){
        if(step==Step.FIRST){
            System.out.println(dividendStr+" | "+divisorStr);
            int spacesLine=position-workPartStr.length();
            String printString=(countSpacesProduct()==0)?Integer.toString(product):
                    String.format("%"+countSpacesProduct()+"s", product);
            System.out.println(printString);
            //i'll change all this method in such style
            System.out.println(addSpaces(spacesLine)+"---");
        }else if(step==Step.OTHER){
            System.out.println(addSpaces(position-workPartStr.length())+workPartStr);
            System.out.println(addSpaces(countSpacesProduct())+product);
            System.out.println(addSpaces(position-workPartStr.length())+"---");
        }else if (step==Step.LAST){
            rest=workPart-product;
            System.out.println(addSpaces(position-Integer.toString(rest).length())+rest);
            System.out.println("\nFinal result is "+finalRes+", remainder of the division is  "+Math.abs(rest));
        }
    }
    static int countSpacesProduct(){
        return position-Integer.toString(product).length();
    }
    static String addSpaces(int number){
        String str="";
        for(int i=0;i<number;i++){
           str+=" ";
        }
        return str;
    }
}
